<template>
	<view class="content">
		<image class="logo" src="/static/logo.png"></image>
		<view class="text-area">
			<text class="title">{{title}}</text>
		</view>
		<view @click="toAuthorizePage()">
			<text>去授权</text>
		</view>
		<view @click="streamData"><text>接收流式数据</text></view>
		<view><text>{{fluxData}}</text></view>
	</view>
</template>

<script>
	import {appId} from '@/utils/config.js'
	
	export default {
		data() {
			return {
				fluxData: '',
				title: 'Hello'
			}
		},
		onLoad() {

		},
		methods: {
			streamData() {
			},
			toAuthPage() {
				uni.navigateTo({
					url: '/pages/login/authorize'
				})
			},
			toAuthorizePage(stateObj = {}) {
				// https://open.weixin.qq.com/connect/oauth2/authorize 是微信公众号官方提供的授权页面
				// 微信授权页面授权成功并获取到code后，自动跳转至redirect_uri/?code=CODE&state=STATE，redirect_uri参数需encodeURIComponent格式化，且注意二级路径
				// 应用授权作用域，snsapi_base直接挑战，snsapi_userinfo需用户显式授权
				// state为跳转至redirect_uri附带的参数，最多128字节
				let redirect_uri = location.origin + location.pathname + '#/pages/login/authorize'
				console.log(redirect_uri)
				let scope = 'snsapi_userinfo'
				let state = btoa(JSON.stringify(stateObj))
				console.log('state', state)
				let authPageHref = 'https://open.weixin.qq.com/connect/oauth2/authorize' +
					'?appid=' + appId +
					'&redirect_uri=' + encodeURIComponent(redirect_uri) +
					'&response_type=code&scope=' + scope +
					'&state=' + state +
					'#wechat_redirect';
				console.log(authPageHref)
				// location.href = authPageHref
			},

		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>
